package com.github.baichuan.web.dto;


import com.github.baichuan.web.data.TimeFormat;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Pattern;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;

@Getter
@Valid
public class DateRange {

    @Pattern(regexp = "\\d{4}-\\d{2}-\\d{2}", message = "日期格式应为yyyy-MM-dd")
    private String begin;

    @Pattern(regexp = "\\d{4}-\\d{2}-\\d{2}", message = "日期格式应为yyyy-MM-dd")
    private String end;

    public LocalDateTime beginDateTime(){
        if(StringUtils.isBlank(begin)){
            return null;
        } else {
            return LocalDate.parse(begin, TimeFormat.LOCAL_DATE).atTime(0, 0, 0);
        }
    }

    public LocalDateTime endDateTime(){
        if(StringUtils.isBlank(end)){
            return null;
        } else {
            return LocalDate.parse(end, TimeFormat.LOCAL_DATE).atTime(23, 59, 59);
        }
    }
}
